// pages/scan/scan.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iosflag: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // var that = this;
    // app.scanFlag = false
    // this.scanBookCode();
  },
  scanBookCode: function() {
    app.scanFlag = false
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        var qcodeurl = res.result
        console.log(qcodeurl)
        var isQcodeurl = qcodeurl.split('?')[0]
        qcodeurl = qcodeurl.split('?')[1]
        console.log(qcodeurl)
        qcodeurl = qcodeurl + "&scanflag=3"
        console.log(qcodeurl)
        if (isQcodeurl == "https://hptupgrade.chinasafe.com.cn/book/isWeChat.do") {
          wx.navigateTo({
            url: "../details/borrow/borrow?" + qcodeurl + "&isBackEstimate=no"
          })
        } else {
          wx.showToast({
            title: "无效二维码",
            icon: "warn",
            image: '../../images/icon/fail.png',
          });
          setTimeout(function() {
            wx.switchTab({
              url: '../library/library'
            })
          }, 700);//无效二维码显示700ms后跳转
        }
      },
      fail(res) {
        console.log('取消跳转')
        wx.switchTab({
          url: '../library/library'
        })
      }
    })
  },
  // onReady: function() {
  //   var that = this;
  //   wx.getSystemInfo({
  //     success(res) {
  //       var os = res.system
  //       console.log(os.toUpperCase())
  //       if (os.toUpperCase().indexOf("IOS") != -1) {
  //         console.log('扫码0')
  //         that.scanBookCode()
  //       }
  //     }
  //   })
  // },
  /** 
   * 生命周期函数--监听页面显示 
   */
  onShow: function() {
    var that = this;
    if (app.scanFlag) {
      setTimeout(function () {
        that.scanBookCode();
      }, 200);
    }
  },
})